from django.db import models


# Create your models here.

class RelationshipState(models.Model):
    """关系状态表"""
    name = models.CharField(verbose_name='客户关系状态', max_length=32)

    def __str__(self):
        return self.name


class Team(models.Model):
    """团队表"""
    name = models.CharField(verbose_name='团队名称', max_length=32)

    def __str__(self):
        return self.name


class secondary_structure(models.Model):
    """二级结构表"""
    name = models.CharField(verbose_name='二级结构名称', max_length=32)

    def __str__(self):
        return self.name


class StaffInfo(models.Model):
    """员工表"""
    staff_num = models.CharField(verbose_name='员工编号', max_length=32)
    name = models.CharField(verbose_name='员工姓名', max_length=32)
    password = models.CharField(verbose_name='员工密码', max_length=32)
    # 权限级别
    level_choices = [
        (1, '普通员工'),
        (2, '团队长'),
        (3, '负责人'),
    ]
    level = models.SmallIntegerField(verbose_name='权限级别', choices=level_choices, default=1)
    secondary_structure = models.ForeignKey(verbose_name='二级结构', to='secondary_structure',default=1, on_delete=models.CASCADE,
                                            null=True, blank=True)
    # 员工所属团队
    team = models.ForeignKey(verbose_name='所属团队', to='Team', on_delete=models.CASCADE, null=True, blank=True)

    customer_count = models.IntegerField(verbose_name='名下客户总数', default=0, null=True, blank=True)

    customer_count_contract = models.IntegerField(verbose_name='已签约客户数量', default=0, null=True, blank=True)

    financial_product_count = models.IntegerField(verbose_name='保证金理财产品签约数', default=0, null=True, blank=True)

    fund_products_count = models.IntegerField(verbose_name='货币基金产品配置客户数数', default=0, null=True, blank=True)

    investment_assets_count = models.IntegerField(verbose_name='固定收益产品配置客户数', default=0, null=True, blank=True)

    add_weChat_count = models.IntegerField(verbose_name='企业微信客户添加数', default=0, null=True, blank=True)

    contract_stock_count = models.IntegerField(verbose_name='股票50签约数', default=0, null=True, blank=True)

    yield_enhanced_products_count = models.IntegerField(verbose_name='量化T+0签约数', default=0, null=True, blank=True)

    equity_public_offering_count = models.IntegerField(verbose_name='权益公募配置客户数', default=0, null=True, blank=True)

    small_t_one_assets_count = models.IntegerField(verbose_name='公募50配置客户数', default=0, null=True, blank=True)

    stock_financing_count = models.IntegerField(verbose_name='两融业务签约数', default=0, null=True, blank=True)

    more_t_one_assets_count = models.IntegerField(verbose_name='百万T1签约数', default=0, null=True, blank=True)

    Private_equity_products_count = models.IntegerField(verbose_name='百万私募签约数', default=0, null=True, blank=True)

    configure_China_count = models.IntegerField(verbose_name='中国50签约数', default=0, null=True, blank=True)

    staff_score = models.IntegerField(verbose_name='员工总工分', default=0, null=True, blank=True)

    # 计算总工分 调用  self.staff_score = self.calculate_staff_score()
    def calculate_staff_score(self):
        self.staff_score = int(self.financial_product_count) * 1 + int(self.fund_products_count) * 1 + int(
            self.investment_assets_count) * 2 + \
                           int(self.add_weChat_count) * 1 + int(self.contract_stock_count) * 2 + int(
            self.yield_enhanced_products_count) * 5 + \
                           int(self.equity_public_offering_count) * 5 + int(self.small_t_one_assets_count) * 5 + int(
            self.stock_financing_count) * 5 + \
                           int(self.more_t_one_assets_count) * 10 + int(self.Private_equity_products_count) * 8 + int(
            self.configure_China_count) * 20
        self.save()
        return self.staff_score

    # 百分比
    @property
    def percent(self):
        if self.customer_count_contract == 0:
            return 0
        else:
            return self.customer_count_contract / self.customer_count * 100

    def __str__(self):
        return self.name


class TeamUser(models.Model):
    """团队成员表"""
    user = models.ForeignKey(verbose_name='用户', to='StaffInfo', on_delete=models.CASCADE)
    team = models.ForeignKey(verbose_name='团队', to='Team', on_delete=models.CASCADE)
    # 创建时间
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)


class CustomerList(models.Model):
    """客户表"""
    oid = models.CharField(verbose_name='客户编号', max_length=32)
    name = models.CharField(verbose_name='客户姓名', max_length=32)

    gender_choices = [
        (1, "男"),
        (2, "女"),
    ]
    gender = models.SmallIntegerField(verbose_name="性别", choices=gender_choices, default=1)

    deposit_amount_choice = [
        (1, "低于30万"),
        (2, "30万——50万"),
        (3, "50万-200万"),
        (4, "超过200万"),
    ]

    deposit_amount = models.SmallIntegerField(verbose_name="资产量", choices=deposit_amount_choice, default=1)

    customer_relationship_type_choice = [
        (1, '主投顾关系'),
        (2, '分配服务关系'),
    ]

    customer_relationship_type = models.SmallIntegerField(verbose_name="客户关系类型",
                                                          choices=customer_relationship_type_choice, default=1)

    customer_relationship_state = models.ForeignKey(verbose_name="客户关系状态", to='RelationshipState', to_field='id',
                                                    on_delete=models.CASCADE, default=1)

    financial_product = models.BooleanField(verbose_name="是否配置了聚金利、中金宝之类的保证金理财产品", default=False)

    fund_products = models.BooleanField(verbose_name="是否配置了大额存单等类货币基金产品", default=False)

    investment_assets = models.BooleanField(verbose_name="是否配置了FI鑫投类和结构化类、债基、中金丰裕类固收以及固收+类产品", default=False)

    add_weChat = models.BooleanField(verbose_name="是否添加企业微信", default=False)

    contract_stock = models.BooleanField(verbose_name="是否签约股票50", default=False)

    yield_enhanced_products = models.BooleanField(verbose_name="是否签约QMT AI T+0等量化交易收益增强的产品", default=False)

    equity_public_offering = models.BooleanField(verbose_name="是否配置权益类公募", default=False)

    small_t_one_assets = models.BooleanField(verbose_name="是否配置公募50或基金投顾和其它不足百万的T1资产", default=False)

    stock_financing = models.BooleanField(verbose_name="是否签约股票两融", default=False)

    more_t_one_assets = models.BooleanField(verbose_name="是否配置微50和其它百万起点的T1资产", default=False)

    Private_equity_products = models.BooleanField(verbose_name="是否配置百万起点的私募产品", default=False)

    configure_China = models.BooleanField(verbose_name="是否配置中国50", default=False)

    # 删除标志
    is_delete = models.BooleanField(verbose_name='是否删除', default=False)

    # 创建时间
    create_time = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    # 修改时间
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)

    # 备注，数据限制在200
    remark = models.CharField(verbose_name='备注', max_length=200, null=True, blank=True)

    # 负责的员工
    staff = models.ForeignKey(verbose_name='负责的员工', to='StaffInfo', on_delete=models.SET_NULL, null=True, blank=True)

    def __str__(self):
        return self.name
